﻿using System;
using System.Reflection;
using System.Windows.Forms;
using NNFramework.NeuroLibrary;

namespace HopFieldTest
{
    public partial class frmNeuronTest : Form
    {
        private clsNeuron<int, int> BasicNeuron;

        public frmNeuronTest()
        {
            InitializeComponent();
            FillActivationFunctions();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            BasicNeuron = CreateNeuron(cmbActivationFunctions.SelectedItem.ToString());
            if (BasicNeuron != null)
            {
                cmbDendrite.Items.Clear();
                for (int i = 0; i < Convert.ToInt32(txtDendrites.Text); i++)
                {
                    BasicNeuron.AddDendrite(1, 1);
                    cmbDendrite.Items.Add(i);
                }
                ShowAxonValue();
                DisplayDendritesValue();
            }
            
        }

        private void btnSetValue_Click(object sender, EventArgs e)
        {
            BasicNeuron.Dendrites[Convert.ToInt32(cmbDendrite.SelectedItem)] = Convert.ToInt32(txtValue.Text);
            BasicNeuron.Weights[Convert.ToInt32(cmbDendrite.SelectedItem)] = Convert.ToDouble(txtWeight.Text);
            ShowAxonValue();
            DisplayDendritesValue();
        }

        private void FillActivationFunctions()
        {   
            Type tpyFunctions = typeof (clsActivationFunctions);
            MethodInfo[] mtiMethods = tpyFunctions.GetMethods();
            foreach (MethodInfo method in mtiMethods)
            {
                if ((method.Name != "ToString") && (method.Name != "Equals") && (method.Name != "GetHashCode") && (method.Name != "GetType"))
                {
                    cmbActivationFunctions.Items.Add(method.Name);
                }
            }
        }

        private static clsNeuron<int,int> CreateNeuron(string pstrFunction)
        {
            clsNeuron<int, int> objResult = null;
            switch (pstrFunction)
            {
                case "StairFunction":
                    objResult = new clsNeuron<int, int>(clsActivationFunctions.StairFunction);
                    break;
                case "PassFunction":
                    objResult = new clsNeuron<int, int>(clsActivationFunctions.PassFunction);
                    break;
                case "AdalineFunction":
                    //The Activation Function isn't appropiate, return an double value
                    //objResult = new clsNeuron<int, int>(clsActivationFunctions.AdalineFunction);
                    MessageBox.Show("The Activation Function isn't appropiate, return an double value");
                    break;
                case "IdentityFunction":
                    objResult = new clsNeuron<int, int>(clsActivationFunctions.IdentityFunction);
                    break;
                case "IdentityFunctionDouble":
                    //The Activation Function isn't appropiate, return an double value
                    //objResult = new clsNeuron<int, int>(clsActivationFunctions.IdentityFunction);
                    MessageBox.Show("The Activation Function isn't appropiate, return an double value");
                    break;
                case "StepFunction":
                    objResult = new clsNeuron<int, int>(clsActivationFunctions.StepFunction);
                    break;
            }
            return objResult;
        }

        private void ShowAxonValue()
        {   
            lblValue.Text = BasicNeuron.Axon.ToString();
        }

        private void DisplayDendritesValue()
        {   
            dgvDendrites.DataSource = BasicNeuron.GetDendrites();
        }

        private void btnViewNeuron_Click(object sender, EventArgs e)
        {
            var frmNeuronView = new frmNeuronViewer(BasicNeuron) {MdiParent = this.MdiParent};
            frmNeuronView.Show();
        }
        
    }
}
